<?php
// +----------------------------------------------------------------------

namespace app\mall\model;

use think\Model as ThinkModel;
use util\Tree;
use think\facade\Db;
/**
 * 仓库模型
 * @package app\mall\model
 */
class Store extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mall_store';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
	
	// 设置json类型字段
	protected $json = ['trace'];
		
    /**
     * 获取仓库列表
     * @return array|mixed
     */
    public static function getList()
    {
        $data_list = self::where('status', 1)->column(true, 'id');
        return $data_list;
    }
	/**
	 * 获取仓库分页列表
	 * @return array|mixed
	 */
	public static function pageList($map = [], $order = [])
	{
	    $data_list = self::where($map)->order($order)->paginate(config('paginate'));
		foreach($data_list as $key => $value){
			$data_list[$key]['nums'] = '库存管理';
		}
	    return $data_list;
	}
    /**
     * 获取指定仓库数据
     * @param int $cid 栏目id
     * @return mixed|static
     */
    public static function getInfo($cid = 0)
    {
        $result = cache('express_info_'. $cid);
        if (!$result) {
            $result = self::get($cid);
            // 非开发模式，缓存数据
            if (config('develop_mode') == 0) {
                cache('express_info_'. $cid, $result);
            }
        }
        return $result;
    }
	
	/**
	 * 执行德邦api操作
	 * @param array $param 数据
	 * @param int $type 类型 1.新订单
	 * @return bool
	 */
	public function deppon($param = [], $type = 1)
	{
	    if($param == []) {
			$this->error = '参数错误';
			return false;
		}
		switch($type){
			case 1:
			$api = config('express.orderApi'); //新订单
			break;
			case 2:
			$api = config('express.queryApi'); //查询
			break;
			case 3:
			$api = config('express.updateApi'); //更新
			break;
			case 4:
			$api = config('express.cancelApi'); //取消
			break;
			case 5:
			$api = config('express.traceApi'); //查询轨迹
			break;
			default:
			$api = config('express.orderApi'); //新下订单
		}
		// 排序
		ksort($param);
		// 毫秒时间戳
		$time = strval(round(microtime(true) * 1000));
		// 签名
		$digest = base64_encode(md5(json_encode($param).config('express.appKey').$time));
		//组建发送参数
		$post = [
			'companyCode'	=> config('express.companyCode'),
			'digest'		=> $digest,
			'timestamp'		=> $time,
			'params'		=> json_encode($param),
		];
		// 附加头部信息
		$header = [
			'content-type:application/x-www-form-urlencoded;charset=utf-8',
		];
		$return = post_curls($api, http_build_query($post), $header);
		return $return;
	}
	
	/**
	 * 运单状态类型
	 * @param int $id 类型id
	 * @author paulxun
	 * @return array|static
	 */
	public static function status($id = 99)
	{
		$types = [
			'GOT' 		=> '开单',
			'ARRIVAL' 	=> '进站',
			'DEPARTURE' => '出站',
			'ADVANCE_DELIVERY' => '预派送',
			'SENT_SCAN' => '派送',
			'SIGNED' 	=> '签收',
			'ERROR' 	=> '滞留',
			'FAILED'	=> '拒签',
		];
	    if ($id === 99) {
	        $result = $types;
	    }else{
			$result = '';
			if(isset($types[$id])) $result = $types[$id];
		}
	    return $result;
	}
	
	/**
	 * 发货仓类型
	 * @param int $id 类型id
	 * @author paulxun
	 * @return array|static
	 */
	public static function type($id = 99)
	{
		$types = [
			1 	=> '发货仓',
			2 	=> '自提点',
		];
	    if ($id === 99) {
	        $result = $types;
	    }else{
			$result = '';
			if(isset($types[$id])) $result = $types[$id];
		}
	    return $result;
	}
	
	/**
	 * 发货仓列表
	 * @author paulxun
	 * @return array|static
	 */
	public static function getStock()
	{
		$data_list = cache('express_list');
		if (!$data_list) {
		    $stock_types = self::type();
		    $data_list = [];
			if ($stock_types){
				foreach($stock_types as $key=> $value){
					$child = self::where('type', $key)->column('title', 'id');
					$child_list = [];
					if ($child) {
						foreach ($child as $k => $v){
							$child_list[] = [
								'label' => $v,
								'value'	=> $k,
							];
						}
					}
					$data_list[] = [
						'label' => $value,
						'value'	=> $key,
						'children'	=> $child_list,
					];
				}
			}
		    cache('express_list', $data_list);
		}
		return $data_list;
	}
}
